package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.MemberListBean;
import com.cljz.makepassenger.ui.activity.InvitationLetterActivity;
import com.cljz.makepassenger.ui.activity.MemberListActivity;
import com.cljz.makepassenger.utils.L;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/5/5.
 */

public class MemberListP extends BaseP<MemberListActivity> {
    public void memberList(int type, String id, int page) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("page", String.valueOf(page));
        if (type == InvitationLetterActivity.INVITE) {
            paramsMap.put("invite_id", id);
        } else if (type == InvitationLetterActivity.ACTIVITY) {
            paramsMap.put("activity_id", id);
        }
        L.e("MemberListP",gson.toJson(paramsMap));
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().memberListData(requestBody)
                .compose(XApi.<MemberListBean>getApiTransformer())
                .compose(XApi.<MemberListBean>getScheduler())
                .compose(getV().<MemberListBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<MemberListBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(MemberListBean memberListBean) {
                        if ("0".equals(memberListBean.getError_code())) {
                            getV().memberListSucceed(memberListBean.getData());
                        } else if ("1000".equals(memberListBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().memberListFail(memberListBean.getMsg());
                        }
                    }
                });
    }
}
